//
//  RGMeDynamicsCell.m
//  RedGrass
//
//  Created by wuyine on 2018/12/28.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGMeDynamicsCell.h"
#import <UIButton+WebCache.h>
#import <UIImageView+WebCache.h>

@implementation RGMeButton
- (instancetype)init {
    if (self = [super init]) {
        self.imageView.contentMode = UIViewContentModeCenter;
        self.titleEdgeInsets = UIEdgeInsetsMake(0,12,0,0);
    }
    return self;
}

@end

@interface RGMeDynamicsCell()
@property (nonatomic,strong) UIButton *headIcon;
@property (nonatomic,copy) UILabel *userName;
@property (nonatomic,copy) UILabel *dateTime;
@property (nonatomic,strong) UIImageView *praiseIcon;
@property (nonatomic,strong) UILabel *praiseNum;
@property (nonatomic,strong) UILabel *content;
@property (nonatomic,strong) RGMeButton *praiseBtn;
@property (nonatomic,strong) RGMeButton *reviewBtn;
@property (nonatomic,strong) RGMeButton *shareBtn;
@property (nonatomic,strong) UIView *line;
@property (nonatomic,strong) UIImageView *contentImage;
@property (nonatomic,strong) UIView *playerView;
@property (nonatomic,strong) UIButton *playBtn;
@property (nonatomic,strong) UIImageView *playStateImg;
@property (nonatomic,strong) UIActivityIndicatorView *activityIndicator;

@end

@implementation RGMeDynamicsCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
        [self initUI];
        [self addMasonry];
    }
    return self;
}

- (CGFloat)cellHeight {
    return 700;
}

- (void)initUI {
    [self.contentView addSubview:self.headIcon];
    [self.contentView addSubview:self.userName];
    [self.contentView addSubview:self.dateTime];
    [self.contentView addSubview:self.praiseIcon];
    [self.contentView addSubview:self.praiseNum];
    [self.contentView addSubview:self.content];
    [self.contentView addSubview:self.contentImage];
    [self.contentView addSubview:self.playerView];
    [self.contentView addSubview:self.playBtn];
    [self.contentView addSubview:self.praiseBtn];
    [self.contentView addSubview:self.reviewBtn];
    [self.contentView addSubview:self.shareBtn];
    [self.contentView addSubview:self.playStateImg];
    [self.contentView addSubview:self.activityIndicator];
    self.activityIndicator.hidden = YES;
    [self.contentView addSubview:self.line];
}

- (void)addMasonry {
    [self.contentView mas_makeConstraints:^(MASConstraintMaker*make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
    [_headIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.leading.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(39, 39));
    }];
    
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.headIcon.mas_trailing).offset(6);
        make.top.equalTo(self.headIcon.mas_top).offset(1);
    }];
    
    [_dateTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.userName.mas_leading);
        make.bottom.equalTo(self.headIcon.mas_bottom).offset(-1);
    }];
    
    [_praiseIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-20);
        make.top.mas_equalTo(5);
        make.size.mas_equalTo(CGSizeMake(18.5, 16.5));
    }];
    
    [_praiseNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.praiseIcon.mas_centerX);
        make.top.equalTo(self.praiseIcon.mas_bottom).offset(3);
    }];
    
    [_content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.top.equalTo(self.headIcon.mas_bottom).offset(15);
        make.trailing.mas_equalTo(-15);
    }];
    NSInteger width = kScreenWidth - 100;
//    NSInteger height = width * 3 / 2;
    
    [_playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.width.mas_equalTo(width);
        make.top.mas_equalTo(self.content.mas_bottom).offset(10);
        make.bottom.mas_equalTo(self.reviewBtn.mas_top).offset(-10);
    }];
    
    [_contentImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.width.mas_equalTo(width);
        make.top.mas_equalTo(self.content.mas_bottom).offset(10);
        make.bottom.mas_equalTo(self.reviewBtn.mas_top).offset(-10);
    }];
    
    [_playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.right.mas_equalTo(self.playerView.mas_right).mas_offset(-4);
        make.bottom.mas_equalTo(self.playerView.mas_bottom).mas_offset(-4);
    }];
    
    [_playStateImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerX.mas_equalTo(self.playBtn.mas_centerX);
        make.centerY.mas_equalTo(self.playBtn.mas_centerY);
//        make.right.mas_equalTo(self.playerView.mas_right).mas_offset(-4);
//        make.bottom.mas_equalTo(self.playerView.mas_bottom).mas_offset(-4);
    }];
    [_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerX.mas_equalTo(self.playBtn.mas_centerX);
        make.centerY.mas_equalTo(self.playBtn.mas_centerY);
    }];
    
    [_reviewBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.playerView.mas_centerX).mas_offset(0);
        make.bottom.mas_equalTo(self.line.mas_bottom).offset(-10);
        make.size.mas_equalTo(CGSizeMake(90, 50));
    }];
    
    [_praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.playerView.mas_leading).mas_offset(0);
//        make.trailing.equalTo(self.reviewBtn.mas_leading).offset(-10);
        make.size.mas_equalTo(CGSizeMake(90, 50));
        make.centerY.equalTo(self.reviewBtn.mas_centerY);
    }];
    
    [_shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.playerView.mas_right).mas_offset(0);
//        make.leading.equalTo(self.reviewBtn.mas_trailing).offset(10);
        make.size.mas_equalTo(CGSizeMake(90, 50));
        make.centerY.equalTo(self.reviewBtn.mas_centerY);
    }];
    
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}

-(void)resizeVideo {
    NSInteger videoWidth = _vedioModel.videoWidth;
    NSInteger videoHeight = _vedioModel.videoHeight;
    NSInteger width = kScreenWidth - 100;
    NSInteger height = width;
    if (videoWidth > 0 && videoHeight > 0) {
        if (videoHeight * 9 > videoWidth * 16) {
            height = width * 16/9;
        } else {
            height = width * videoHeight/videoWidth;
        }
        [_playerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(width, height));
        }];
    }
}

#pragma mark - public

-(void) setVedioModel:(RGRecommendVideoModel *)vedioModel{
    if(vedioModel == nil){
        return;
    }
    _vedioModel = vedioModel;
//    [self resizeVideo];
    [_headIcon sd_setImageWithURL:[NSURL URLWithString:SafeStr(vedioModel.videoUser.image)] forState:UIControlStateNormal placeholderImage:IMAGE_NAMED(@"头像3")];
    _userName.text = SafeStr(vedioModel.videoUser.nickname);
    _dateTime.text = [self makeCustomDateStringWithTime:vedioModel.createTime];
    _content.text = vedioModel.videoDesc;
    [_praiseBtn setTitle:[self makeCountStrWithNumber:vedioModel.likeCounts] forState:UIControlStateNormal];
    [_reviewBtn setTitle:[self makeCountStrWithNumber:vedioModel.commentsCounts] forState:UIControlStateNormal];
    [_shareBtn setTitle:[self makeCountStrWithNumber:vedioModel.forwardCounts] forState:UIControlStateNormal];
    _praiseBtn.selected = vedioModel.isLike ? YES : NO;
    self.loading = NO;
    self.isShowPlayBtn = NO;
    [_contentImage sd_setImageWithURL:[NSURL URLWithString:SafeStr(vedioModel.coverPath)]];
    
}

-(NSString *)makeCustomDateStringWithTime:(NSString *)time{
    NSString *resutStr = time;
    if([time length] == 13){
        NSTimeInterval time1 = [time doubleValue] / 1000;
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time1];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];//创建一个日期格式化器
        dateFormatter.dateFormat=@"MM月dd日 HH:mm";
         resutStr = [dateFormatter stringFromDate:date];
    }else if ([time length] == 10){
        NSTimeInterval time1 = [time doubleValue];
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:time1];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];//创建一个日期格式化器
        dateFormatter.dateFormat=@"MM月dd日 HH:mm";
         resutStr = [dateFormatter stringFromDate:date];
    }
    return resutStr;
}


-(void)updateFaroviteStauts{
      [_praiseBtn setTitle:[self makeCountStrWithNumber:self.vedioModel.likeCounts] forState:UIControlStateNormal];
     _praiseBtn.selected = self.vedioModel.isLike ? YES : NO;
}

-(void)updateCommentCounts{
    [_reviewBtn setTitle:[self makeCountStrWithNumber:self.vedioModel.commentsCounts] forState:UIControlStateNormal];
}

-(void)updateForwardCounts{
     [_shareBtn setTitle:[self makeCountStrWithNumber:self.vedioModel.forwardCounts] forState:UIControlStateNormal];
}

-(UIView *)vedioGroudView{
    return self.playerView;
}

-(NSString *)vedioUrl{
    return _vedioModel.videoPath;
}

-(void) setIsShowPlayBtn:(BOOL)isShowPlayBtn{
    _isShowPlayBtn = isShowPlayBtn;
    self.loading = NO;
    if(isShowPlayBtn){
        _playStateImg.image = IMAGE_NAMED(@"vedio_play_icon");
    }else{
        _playStateImg.image = IMAGE_NAMED(@"vedio_pause_icon");
    }
}

-(void)setLoading:(BOOL)loading {
    _loading = loading;
    if(_loading){
        _activityIndicator.hidden = NO;
        _playStateImg.hidden = YES;
        [_activityIndicator startAnimating];
    } else {
        _playStateImg.hidden = NO;
        [_activityIndicator stopAnimating];
    }
}

#pragma mark - private
-(NSString *)makeCountStrWithNumber:(NSInteger)num{
    if(num < 10000){
        return [NSString stringWithFormat:@"%ld",num];
    }
    float tempNum = num / 10000;
    return [NSString stringWithFormat:@"%0.1fW",ceil(tempNum)];
}

#pragma mark - reponse

- (void)praise:(UIButton *)btn {
    if (self.praiseClick) {
        self.praiseClick();
    }
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kFocusListFocusTag);
    }
}

- (void)review:(UIButton *)btn {
    if (self.reviewClick) {
        self.reviewClick();
    }
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kFocusListCommentTag);
    }
}

- (void)share:(UIButton *)btn {
    if (self.shareClick) {
        self.shareClick();
    }
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kFocusListShareTag);
    }
}

-(void) headIconClick:(UIButton *)button{
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kFocusListUserIconTag);
    }
}

-(void)playBtnClick:(UIButton *)button{
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kFocusListPlayBtnTag);
    }
    
}

#pragma mark - getter
- (UIButton *)headIcon {
    if (!_headIcon) {
        _headIcon = [UIButton buttonWithType:UIButtonTypeCustom];
        [_headIcon setImage:IMAGE_NAMED(@"头像3") forState:UIControlStateNormal];
        [_headIcon addTarget:self action:@selector(headIconClick:) forControlEvents:UIControlEventTouchUpInside];
        _headIcon.layer.cornerRadius = 18.5;
        _headIcon.layer.masksToBounds = YES;
    }
    return _headIcon;
}

- (UILabel *)userName {
    if (!_userName) {
        _userName = [[UILabel alloc] init];
        _userName.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        _userName.font = FONT(customFontNameLight, 14);
        _userName.text = @"用户名";
    }
    return _userName;
}

- (UILabel *)dateTime {
    if (!_dateTime) {
        _dateTime = [[UILabel alloc] init];
        _dateTime.textColor = UICOLOR_WITH_RGBINT(0x999999);
        _dateTime.font = FONT(customFontNameLight, 12);
        _dateTime.text = @"7小时前";
    }
    return _dateTime;
}

- (UIImageView *)praiseIcon {
    if (!_praiseIcon) {
        _praiseIcon = [[UIImageView alloc] init];
        _praiseIcon.image = IMAGE_NAMED(@"动态_点赞2");
        _praiseIcon.hidden = YES;
    }
    return _praiseIcon;
}

- (UILabel *)praiseNum {
    if (!_praiseNum) {
        _praiseNum = [[UILabel alloc] init];
        _praiseNum.textColor = UICOLOR_WITH_RGBINT(0x666666);
        _praiseNum.font = FONT(customFontNameLight, 12);
    }
    return _praiseNum;
}

- (UILabel *)content {
    if (!_content) {
        _content = [[UILabel alloc] init];
        _content.textColor = UICOLOR_WITH_RGBINT(0xffffff);
        _content.font = FONT(customFontNameLight, 15);
        _content.numberOfLines = 0;
    }
    return _content;
}

-(UIActivityIndicatorView *)activityIndicator {
    if(!_activityIndicator) {
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhite)];
        _activityIndicator.hidesWhenStopped = YES;
    }
    return _activityIndicator;
}

-(UIImageView *)playStateImg {
    if (!_playStateImg) {
        _playStateImg = [[UIImageView alloc] init];
        _playStateImg.layer.masksToBounds = YES;
        _playStateImg.contentMode = UIViewContentModeScaleToFill;
        _playStateImg.userInteractionEnabled = false;
        _playStateImg.image = IMAGE_NAMED(@"vedio_pause_icon");
    }
    return _playStateImg;
}

- (UIImageView *)contentImage {
    if (!_contentImage) {
        _contentImage = [[UIImageView alloc] init];
        _contentImage.backgroundColor = [UIColor blackColor];
        _contentImage.contentMode = UIViewContentModeScaleAspectFill;
        _contentImage.layer.masksToBounds = YES;
//        _contentImage.image = IMAGE_NAMED(@"视频图");
        _contentImage.layer.cornerRadius = 6;
    }
    return _contentImage;
}

- (RGMeButton *)praiseBtn {
    if (!_praiseBtn) {
        _praiseBtn = [[RGMeButton alloc] init];
        [_praiseBtn setImage:IMAGE_NAMED(@"动态_点赞") forState:UIControlStateNormal];
        [_praiseBtn setImage:IMAGE_NAMED(@"动态_点赞2") forState:UIControlStateSelected];
//        [_praiseBtn setTitle:@"4.8w" forState:UIControlStateNormal];
        _praiseBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [_praiseBtn addTarget:self action:@selector(praise:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _praiseBtn;
}

- (RGMeButton *)reviewBtn {
    if (!_reviewBtn) {
        _reviewBtn = [[RGMeButton alloc] init];
        [_reviewBtn setImage:IMAGE_NAMED(@"动态_评论") forState:UIControlStateNormal];
//        [_reviewBtn setTitle:@"589" forState:UIControlStateNormal];
        [_reviewBtn addTarget:self action:@selector(review:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _reviewBtn;
}

- (RGMeButton *)shareBtn {
    if (!_shareBtn) {
        _shareBtn = [[RGMeButton alloc] init];
        [_shareBtn setImage:IMAGE_NAMED(@"动态_分享") forState:UIControlStateNormal];
        [_shareBtn setTitle:@"分享" forState:UIControlStateNormal];
        _shareBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_shareBtn addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _shareBtn;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UICOLOR_WITH_RGBINT(0x464350);
    }
    return _line;
}

-(UIView*)playerView{
    if(!_playerView){
        _playerView = [[UIView alloc]init];
        _playerView.backgroundColor = [UIColor clearColor];
        _playerView.layer.masksToBounds = YES;
        _playerView.layer.cornerRadius = 6;
    }
    return _playerView;
}

-(UIButton *)playBtn{
    if(!_playBtn){
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_playBtn setImage:IMAGE_NAMED(@"vedio_pause_icon") forState:UIControlStateNormal];
//        [_playBtn setImage:IMAGE_NAMED(@"vedio_play_icon") forState:UIControlStateSelected];
        [_playBtn addTarget:self action:@selector(playBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

@end
